 package example9_GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.border.EmptyBorder;

import example9_GUI.HandleListener;
import example9_GUI.TextListener;
import javax.swing.JTextField;
import javax.swing.JLabel;

public class WindowDocument extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					WindowDocument frame = new WindowDocument();
					frame.setVisible(true);
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	JTextArea inputText,showText; 
	JMenuBar menubar;
	JMenu menu;
	JMenuItem  itemCopy,itemCut,itemPaste,itemSave,itemOpen,itemReplace,itemCalc; 
	TextListener textChangeListener;  //inputText�ļ�����
	HandleListener handleListener;  //itemCopy,itemCut,itemPaste�ļ�����
	JTextField replaceField;
	JTextField beReplacedField;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	public WindowDocument() {
		init();
	    setVisible(true);
	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
	}
	void init() {
	      inputText = new JTextArea(16,28);
	      showText = new JTextArea(16,28);
	      showText.setLineWrap(true);       //�ı��Զ�����
	      showText.setWrapStyleWord(true);  //�ı����Ե���Ϊ���Զ�����
	      menubar=new JMenuBar(); 
	      menu=new JMenu("菜单");   
	      itemCopy=new JMenuItem("复制(C)");
	      itemCut=new JMenuItem("剪切(T)");
	      itemPaste=new JMenuItem("粘贴(P)");
	      itemSave=new JMenuItem("保存(S)");
	      itemOpen=new JMenuItem("打开(K)");//
	      itemReplace=new JMenuItem("替换(K)");
	      itemCalc=new JMenuItem("统计(C)"); 
	      itemCopy.setAccelerator(KeyStroke.getKeyStroke('c'));//���ÿ�ݷ�ʽ
	      itemCut.setAccelerator(KeyStroke.getKeyStroke('t'));
	      itemPaste.setAccelerator(KeyStroke.getKeyStroke('p'));//���ÿ�ݷ�ʽ
	      itemSave.setAccelerator(KeyStroke.getKeyStroke('s'));
	      itemOpen.setAccelerator(KeyStroke.getKeyStroke('k'));//
	      itemReplace.setAccelerator(KeyStroke.getKeyStroke('r'));
	      itemCalc.setAccelerator(KeyStroke.getKeyStroke('c'));
	      itemCopy.setActionCommand("copy");
	      itemCut.setActionCommand("cut");
	      itemPaste.setActionCommand("paste");
	      itemSave.setActionCommand("save");
	      itemOpen.setActionCommand("open");//
	      itemReplace.setActionCommand("replace");
	      itemCalc.setActionCommand("calc");
	      menu.add(itemCopy);
	      menu.add(itemCut);
	      menu.add(itemPaste);
	      menu.add(itemSave);
	      menu.add(itemOpen);
	      menu.add(itemReplace);
	      menu.add(itemCalc);
	      menubar.add(menu);
	      setJMenuBar(menubar);
	      getContentPane().setLayout(null);
	      JScrollPane scrollPane = new JScrollPane(inputText);
	      scrollPane.setBounds(12, 5, 202, 184);
	      getContentPane().add(scrollPane);
	      JScrollPane scrollPane_1 = new JScrollPane(showText);
	      scrollPane_1.setBounds(219, 5, 202, 326);
	      getContentPane().add(scrollPane_1);
	      textChangeListener = new TextListener();
	      handleListener = new HandleListener(); 
	      textChangeListener.setInputText(inputText);
	      textChangeListener.setShowText(showText);  
	      handleListener.setInputText(inputText);
	      handleListener.setShowText(showText);
	 
	      replaceField = new JTextField();
	      replaceField.setBounds(12, 219, 188, 46);
	      getContentPane().add(replaceField);
	      replaceField.setColumns(10);
	      handleListener.setReplaceField(replaceField);//
	      
	      beReplacedField = new JTextField();
	      beReplacedField.setColumns(10);
	      beReplacedField.setBounds(12, 285, 188, 46);
	      handleListener.setReplacedField(beReplacedField);//
	      getContentPane().add(beReplacedField);
	      
	      lblNewLabel = new JLabel("替换");
	      lblNewLabel.setBounds(78, 199, 54, 15);
	      getContentPane().add(lblNewLabel);
	      
	      lblNewLabel_1 = new JLabel("被替换");
	      lblNewLabel_1.setBounds(78, 270, 54, 15);
	      getContentPane().add(lblNewLabel_1);
	      (inputText.getDocument()).addDocumentListener(textChangeListener);//���ĵ�ע�������
	      itemCopy.addActionListener(handleListener);                       //��˵���ע�������
	      itemCut.addActionListener(handleListener);
	      itemPaste.addActionListener(handleListener);
	      itemSave.addActionListener(handleListener);
	      itemOpen.addActionListener(handleListener);
	      itemReplace.addActionListener(handleListener);
	      itemCalc.addActionListener(handleListener);
	   }
}
